home *** CD-ROM | disk | FTP | other *** search
/ Super PC 34 / Super PC 34 (Shareware).iso / spc / UTIL / DJGPP2 / V2 / DJLSR200.ZIP / src / libc / dos / compat / d_write.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-09  |  1.7 KB  |  61 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. /*
  3.  * D_WITE.C.
  4.  *
  5.  * Written by Peter Sulyok 1995 <sulyok@math.klte.hu>.
  6.  *
  7.  * This file is distributed WITHOUT ANY WARRANTY; without even the implied
  8.  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9.  *
  10.  */
  11.  
  12. #include <libc/stubs.h>
  13. #include <libc/dosio.h>
  14. #include <go32.h>
  15. #include <dpmi.h>
  16. #include <errno.h>
  17. #include <dos.h>
  18.  
  19. unsigned int _dos_write(int handle, const void *buffer, unsigned int count, unsigned int *result)
  20. {
  21.   __dpmi_regs r;
  22.   unsigned int dos_segment, dos_selector, dos_buffer_size, write_size;
  23.   const unsigned char *p_buffer;
  24.  
  25.   /* Allocates ~64K or less transfer buffer from DOS */
  26.   dos_buffer_size = ( count < 0xFFE0 ) ? count : 0xFFE0;
  27.   if ( (dos_segment=__dpmi_allocate_dos_memory((dos_buffer_size + 15) >> 4, &dos_selector)) == -1 )
  28.   {
  29.     errno = ENOMEM;
  30.     return 8;
  31.   }
  32.  
  33.   /* Reads blocks from user buffer and writes to file. */
  34.   p_buffer = (const unsigned char *)buffer;
  35.   *result  = 0;
  36.   while( count )
  37.   {
  38.     write_size = ( count < dos_buffer_size ) ? count : dos_buffer_size;
  39.     movedata(_my_ds(), (unsigned int)p_buffer, dos_selector, 0, write_size);
  40.     r.h.ah = 0x40;
  41.     r.x.bx = handle;
  42.     r.x.cx = write_size;
  43.     r.x.ds = dos_segment;
  44.     r.x.dx = 0;
  45.     __dpmi_int(0x21, &r);
  46.     if ( r.x.flags & 1 )
  47.     {
  48.       __dpmi_free_dos_memory(dos_selector);
  49.       errno = __doserr_to_errno(r.x.ax);
  50.       return r.x.ax;
  51.     }
  52.     count    -= write_size;
  53.     p_buffer += write_size;
  54.     *result  += r.x.ax;
  55.   }
  56.  
  57.   /* Frees allocated DOS transfer buffer. */
  58.   __dpmi_free_dos_memory(dos_selector);
  59.   return 0;
  60. }
  61.